package com.housekeeping.housekeeping.cleanerInformation.controller;

import com.housekeeping.housekeeping.cleanerInformation.utils.ResponseImages;
import com.housekeeping.housekeeping.cleanerInformation.utils.ResponseImagesUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author hua
 * @version JDK1.8
 * @date 2021/1/21 15:54
 *      图片上传
 */
@WebServlet("/cleanerInformation/controller/CleanerImagesController")
public class CleanerImagesController extends HttpServlet {
    public static String portrait = "";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //1、创建一个磁盘工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //2、创建上传对象，传递工厂对象
        ServletFileUpload upload = new ServletFileUpload(factory);
        //图片在服务器中的的物理路径


        //3、通过上传对象解析请求
        List<FileItem> list = null;
        try {
            list = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        //4、遍历集合
        for (FileItem f:list) {
            //5、f.isFormField()判断是否为二进制文件，如果是二进制文件返回false，不是二进制文件返回true
            //此处需要的是二进制文件，所以取反
            if(!f.isFormField()){
                //6、获取输入流
                InputStream is = f.getInputStream();
                //7、创建输出流
                //获取当前项目的路径
                String path = getServletContext().getRealPath("images");
                /*String path = "F:\\ideaIU-2019.1.3\\code\\housekeeping\\src\\main\\webapp\\images";*/
                //拼接文件名
                UUID uuid = UUID.randomUUID();
                String fileName = uuid.toString()+"_"+f.getName();
                System.out.println(path);
                //创建一个文件对象File
                //8、判断存放上传文件的文件目录是否存在
                //如果不存在就创建
                File file = new File(path);
                //exists() 该文件的路径是否存在，如果存在返回true，不存在返回false
                if (!file.exists()){
                    //文件路径不存在，创建该路径
                    file.mkdirs();
                }
                //创建输出流，参数1文件目录，参数2文件名字
                FileOutputStream fos = new FileOutputStream(new File(path,fileName));
                FileOutputStream fosCopy = new FileOutputStream(new File("F:\\ideaIU-2019.1.3\\code\\housekeeping\\src\\main\\webapp\\images",fileName));
                //图片的绝对路径
                portrait = ""+"/images/"+fileName;
                System.out.println(portrait);
                //9、拷贝
                byte[] bytes = new byte[1024];
                int len;
                while ((len = is.read(bytes)) != -1){
                    //将读到的数据写入文件中，参数1存放数据的数组，参数2偏移量，参数3数据的长度
                    fos.write(bytes,0,len);
                    fosCopy.write(bytes,0,len);
                }
                //关流
                is.close();
                fos.close();
                fosCopy.close();
            }
        }

        ResponseImages<Object> images = ResponseImages.success(null);
        ResponseImagesUtils.responseImages(response,images);


    }
}
